﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类class。
    /// </summary>
    public class Class
    {
        public Class()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_class where [id]=" + id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.Class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [lt_class](");
            strSql.Append("title,style,[time],daima,ren,overtime,kg");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.title + "',");
            strSql.Append("'" + model.style + "',");
            strSql.Append("'" + model.time + "',");
            strSql.Append("'" + model.daima + "',");
            strSql.Append("'" + model.ren + "',");
            strSql.Append("'" + model.overtime + "',");
            strSql.Append("" + model.kg + "");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.Class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [lt_class] set ");
            if (model.title != null)
                strSql.Append("title='" + model.title + "',");
            if (model.style != null)
                strSql.Append("style='" + model.style + "',");
            if (model.daima != null)
                strSql.Append("daima='" + model.daima + "',");
            if (model.ren != null)
                strSql.Append("ren='" + model.ren + "',");
            if (model.overtime != null)
                strSql.Append("overtime='" + model.overtime + "',");
            //if (model.kg.Equals(Int32.Parse("0")) != true)
            strSql.Append("kg=" + model.kg + ",");
            strSql.Append("[time]=[time] ");
            strSql.Append(" where [id]=" + model.id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [lt_class] ");
            strSql.Append(" where [id]=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.Class GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [id],[title],[style],[time],[daima],[ren],[overtime],[kg] ");
            strSql.Append(" from [lt_class] ");
            strSql.Append(" where [id]=" + id);
            ltModel.Model.Class model = new ltModel.Model.Class();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.id = id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.title = ds.Tables[0].Rows[0]["title"].ToString();
                model.style = ds.Tables[0].Rows[0]["style"].ToString();
                model.time = ds.Tables[0].Rows[0]["time"].ToString();
                model.daima = ds.Tables[0].Rows[0]["daima"].ToString();
                model.ren = ds.Tables[0].Rows[0]["ren"].ToString();
                model.overtime = ds.Tables[0].Rows[0]["overtime"].ToString();
                if (ds.Tables[0].Rows[0]["kg"].ToString() != "")
                {
                    model.kg = int.Parse(ds.Tables[0].Rows[0]["kg"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],[title],[style],[time],[daima],[ren],[overtime],[kg] ");
            strSql.Append(" FROM [lt_class] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        public DataSet GetList(string top,string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top " + top + "  [id],[title],[style],[time],[daima],[ren],[overtime],[kg] ");
            strSql.Append(" FROM [lt_class] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }



        #endregion  成员方法
    }
}